package com.clps.algorithm03.链表;

public class Stack2 <E>{

    private Object[] data;
    private int maxSize;//栈的最大容量
    private int top = -1;//栈顶元素小标

    public Stack2(int maxSize) {
       if(maxSize <=0){
        throw  new IllegalStateException("队列容量必须大于0"+maxSize);
       }
       this.maxSize = maxSize;
       data = new Object[this.maxSize];
    }

    //往栈中添加元素
    public void  push(E val){
        if(top == maxSize-1){
            throw  new IllegalStateException("栈已经满了,无法入栈了");
        }
        data[++top]= val;
    }

    //弹出栈顶的元素
    public E pop(){
        if(top == -1){
            throw  new IllegalStateException("栈是空的,无法出栈");
        }
        E result= (E) data[top];
        data[top--] = null;
        return  result;
    }


    //获取栈顶的元素的值,不出栈
    public  E top(){
        if(top == -1){
            throw  new IllegalStateException("栈是空的,无法出栈");
        }
        return (E) data[top];
    }
}
